const GameData = require("./GameData");

cc.Class({
    extends: cc.Component,
    properties: {
        
        mHook: {
            default: null,
            type:cc.Node,
        },

        mSpeed: 350,
        mMoveToPos: {
            default:cc.v2(0,0),
            visible:false,
        },

        mCanvas:{
            default:null,            
            type:cc.Node,
        },

        mIsMoving:{
            default: false,
            visible: false,
        },

        mEnableTouch:{
            default:false,
            visible:false,
        },


    },

    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {

        // let Canvas = cc.find("Canvas")=
        // this.node.runAction(cc.repeatForever(cc.moveBy(5,cc.v2(0,-100))));    
        this.enableTouch();
    },

    // update (dt) {},

    // touch 放到这里好么？？

    enableTouch() {
        if(this.mEnableTouch) 
            return;
        this.mHook.on(cc.Node.EventType.TOUCH_START,this.onTouchStart,this);
        this.mHook.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchMove,this);
        this.mHook.on(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this);
        this.mHook.on(cc.Node.EventType.TOUCH_CANCEL,this.onTouchEnd,this);
        this.mEnableTouch = true;
        console.log("EnableTouch");

    },

    disableTouch() {
        if(!this.mEnableTouch)
            return;
        this.mHook.off(cc.Node.EventType.TOUCH_START,this.onTouchStart,this);
        this.mHook.off(cc.Node.EventType.TOUCH_MOVE,this.onTouchMove,this);
        this.mHook.off(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this);
        this.mHook.off(cc.Node.EventType.TOUCH_CANCEL,this.onTouchEnd,this);
        this.mEnableTouch = true;
        console.log("disableTouch");
    },


    onTouchStart(event) {

        let touches = event.getTouches();
        let touchLoc = touches[0].getLocation();
        this.mIsMoving = true;
        this.mMoveToPos = this.node.parent.convertToNodeSpaceAR(touchLoc);  //
    },

    onTouchMove(event){

        let touches = event.getTouches();
        var touchLoc = touches[0].getLocation();
        this.mMoveToPos = this.node.parent.convertToNodeSpaceAR(touchLoc);  //
    },

    onTouchEnd(event){

        this.mIsMoving = false;
    },    

    update(dt){

        if(! this.mIsMoving)
            return;
        
        
        let oldPos = this.node.position;
        let dir = this.mMoveToPos.sub(oldPos).normalize();
        dir.y = 0;

        let newPos = oldPos.add(dir.mul(this.mSpeed * dt));
        this.node.setPosition(newPos);        
    },


    onCollisionEnter(other,self){
        cc.log("onCollisionEnter(other,self)");
        other.node.color = cc.Color.RED;                
        // self.color = cc.Color.BLUE;

        let hook = this.mHook.getComponent('hook');
        hook.regainLine();

        other.node.stopAllActions();
        other.node.group = "default";
        other.node.parent = this.node;
        other.node.setPosition(cc.v2(0,0));
        other.node.runAction(cc.repeatForever(cc.sequence(
            cc.rotateTo(0.5,-60 * other.node.scaleX),
            cc.rotateTo(0.5,-30 * other.node.scaleY)
        )));
        GameData.instance.score += 10; // 每条鱼加10分

    }
});
